package utils

import (
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
	"math"
	"strings"
)

type timeFormatStruct struct{
	CalcNum1 	int64			// 1类型计算的基数 1为除(必需大于0)
	CalcNum2 	int64			// 2类型计算的基数 2为取余
	DataList 	[]timeFormatStructMsg
}
type timeFormatStructMsg struct{
	ReplaceStr 	string
	ShowNum 	int
	CalcType 	int		// 计算类型:1为除 2为取余
}
// 时间处理默认方式
func FormatTimeSecondAutoDefalut(sec int64) string  {
	return FormatTimeSecondAuto(sec, "d天HH小时MM分SS秒", false)
}
// sec:秒
// format:例如 hh或HH小时  25或01小时
func FormatTimeSecondAuto(sec int64, formatStr string, isShowAll bool) string  {
	list := []timeFormatStruct{
		timeFormatStruct{
			1,
			60,
			[]timeFormatStructMsg{
				timeFormatStructMsg{ReplaceStr: "ss", ShowNum: 2, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "s", ShowNum: 1, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "SS", ShowNum: 2, CalcType: 2},
				timeFormatStructMsg{ReplaceStr: "S", ShowNum: 1, CalcType: 2},
			},
		},
		timeFormatStruct{
			60,
			3600,
			[]timeFormatStructMsg{
				timeFormatStructMsg{ReplaceStr: "mm", ShowNum: 2, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "m", ShowNum: 1, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "MM", ShowNum: 2, CalcType: 2},
				timeFormatStructMsg{ReplaceStr: "M", ShowNum: 1, CalcType: 1},
			},
		},
		timeFormatStruct{
			3600,
			86400,
			[]timeFormatStructMsg{
				timeFormatStructMsg{ReplaceStr: "hh", ShowNum: 2, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "h", ShowNum: 1, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "HH", ShowNum: 2, CalcType: 2},
				timeFormatStructMsg{ReplaceStr: "H", ShowNum: 1, CalcType: 2},
			},
		},
		timeFormatStruct{
			86400,
			1,
			[]timeFormatStructMsg{
				timeFormatStructMsg{ReplaceStr: "dd", ShowNum: 2, CalcType: 1},
				timeFormatStructMsg{ReplaceStr: "d", ShowNum: 1, CalcType: 1},
			},
		},
	}
	timeStr := ""
	for _, dataStruct := range list {
		for _, timeData := range dataStruct.DataList {
		  	isHave, addRepStr, tmpFormat := formatTimeSecondAutoFormatHandle(formatStr, &timeData)
			formatStr = tmpFormat
		  	if (isHave == false) {
		  		continue;
			}

			if (isShowAll == false && sec / dataStruct.CalcNum1 < 1) {
				break
			}

			var value1 int64
			if (timeData.CalcType == 1) {
				value1 = sec
			} else {
				value1 = sec % dataStruct.CalcNum2
			}
			tmpValue := math.Floor(gconv.Float64(value1) / gconv.Float64(dataStruct.CalcNum1))
			valueStr := gconv.String(tmpValue)
			timeStr = copyStrNum(timeData.ShowNum - len(valueStr)) + valueStr + addRepStr + timeStr
		}
	}
	return timeStr
}

func copyStrNum(lenNum int)(str string) {
	if (lenNum < 1) {
		return str
	}
	str += "0"
	return copyStrNum(lenNum -1)
}

// 时间秒自动化格式处理
func formatTimeSecondAutoFormatHandle(format string, params *timeFormatStructMsg) (bool, string, string) {
	if(strings.Index(format, params.ReplaceStr) == -1) {
		return false, "", format
	}
	list := strings.Split(format, params.ReplaceStr)
	return true, list[1], list[0]
}

// 获取当前时间戳
func GetTimestamp() int64 {
	return gtime.Timestamp()
}
